package cn.bopomo.dealer.base.db.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import cn.bopomo.dealer.base.db.BaseDAO;
import cn.bopomo.dealer.base.db.IModuleDAO;
import cn.bopomo.dealer.base.exception.ExceptionCode;
import cn.bopomo.dealer.base.exception.SystemException;
import cn.bopomo.dealer.base.util.Page;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class ModuleDAOImpl extends BaseDAO implements IModuleDAO {

	public String insertModule(ModuleList module) {

		try {
			this.insertOrUpdate(module);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String updateModule(ModuleList module) {
		try {
			this.insertOrUpdate(module);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public String deleteModule(ModuleList module) {
		try {
			this.getHibernateTemplate().delete(module);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.logger.warn(e);
			return ResultCode.OP_FAILED;
		}
		return ResultCode.OP_SUCCESS;
	}

	public ModuleList selectModule(String tag) throws SystemException {

		String hql = "from ModuleList ";
		
		Object module = null;
		try {
			module = this.sectionFirstObject(hql,"where moduleTag = ?", tag);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (module != null){
			return (ModuleList)module;
		}
		return null;
		
	}

	public ModuleList selectModule(int uId) throws SystemException {
		// TODO Auto-generated method stub
		String hql = "from ModuleList ";
		Object module = null;
		try {
			OperatorDetail od = new OperatorDetail();
			od.setId(new Integer(uId));
			module = this.sectionFirstObject(hql,"where operatorDetail = ?", od);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (module != null){
			return (ModuleList)module;
		}
		return null;
	}

	public ModuleList selectModule(Integer mId) throws SystemException {
		
		String hql = "from ModuleList ";
		
		Object module = null;
		try {
			module = this.sectionFirstObject(hql,"where mid = ?", mId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (module != null){
			return (ModuleList)module;
		}
		return null;
	}
	
	public ModuleList selectModuleByName(String name) throws SystemException {

		String hql = "from ModuleList ";
		
		Object module = null;
		try {
			module = this.sectionFirstObject(hql,"where moduleName = ?", name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new SystemException(ExceptionCode.S_100000,"数据库发生数据异常");
		}
		if (module != null){
			return (ModuleList)module;
		}
		return null;
		
	}

	public List selectModules() throws SystemException {
		// TODO Auto-generated method stub
		String hql = "from ModuleList ";
		return this.selectAll(hql,null);
	}

	public Page selectModules(int pageSize, int rowsPerPage) throws Exception {
		// TODO Auto-generated method stub
		String hql = "from ModuleList ";
		return this.selectSectionData(hql,null,null,null,"modifyDate",true,pageSize,rowsPerPage);
	}

	public List selectModules(final String tags){
		final String sql = "from ModuleList where moduleTag in (?)";
		
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(sql);
				
				query.setParameter(0, tags);

				return query.list();
			}
		});
	}
	
}
